#include <SoftwareSerial.h>


int green = 10;
int red = 11;

// 双色灯状态
int payload = 0;
int statu = 0;

SoftwareSerial ESP(6, 5);
#define USE_SERIAL Serial
void setup() {
  
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);
  // 初始化软串口通信
  ESP.begin(115200);
  USE_SERIAL.begin(115200);
  // 监听软串口
  ESP.listen();
}

void loop() {
  payload = 0;
//  while(ESP.available() > 0)
//  {
//    char col = ESP.read();
//    payload += (char)col;
//  }
  

  if(ESP.available())
  {
    payload = ESP.read();
  }
  USE_SERIAL.println(payload);
  if(payload == 49 && statu != 1)
  {
    USE_SERIAL.println("开绿");
    statu = 1;
    analogWrite(green,255);
  }
  else if(payload == 50 && statu != 2)
  {
    USE_SERIAL.println("关绿");
    statu = 2;
    analogWrite(green,0);
  }
  else if(payload == 51 && statu != 3)
  {
    USE_SERIAL.println("开红");
    statu = 3;
    analogWrite(red,255);
  }
  else if(payload == 52 && statu != 4)
  {
    USE_SERIAL.println("关红");
    statu = 4;
    analogWrite(red,0);
  }
  delay(1000);
}
